Crime Rates in Boston

Imagine that you have been tasked with exploring the crime rates in different districts in the city of Boston through the police dataset. The specific districts we will be looking at is B2, B3, and D14. Some stats about main communities within these districts are shown below:

Mattapan and Roxbury (Districts B2 and B3)

Middlesex (District D14)

The findings for this project will be useful in uncovering certain trends of crime throughout Boston and the communities within it. The informaiton here could determine where certain resources will be distributed within the Boston government for reducing crime. Good luck!

Jupyter Notebook

First things first, let's get some terminology straight.

Jupyter notebooks have a few special properties that make it ideal for work with data:

Anything you can do in Python, you can do here!

  1. Write a function that takes a string as input, and does something to it
  2. In a new cell, call the function and test it out

Importing packages

We use the pandas package to easily work with data as tables.
The numpy package allows us to work with some other special data types, like missing values

We'll rename these as pd and np, just so its easier to refer to later on

Importing data

For this semester, we'll typically work with data in tabular format, the type you'd be used to in an excel spreadsheet. Data files saved in this format will usually have a .csv file ending, short for comma seperated values.

For example, a CSV file could look something like...

INCIDENT_NUMBER, OFFENSE_DESCRIPTION, DISTRICT, SHOOTING,
PLTEST005, BURGLARY - RESIDENTIAL, B2 , True
PLTEST003, INVESTIGATE PROPERTY, B2, False
PLTEST002, INVESTIGATE PROPERTY, B2, False

To import this, let's use the pd.read_csv() function:

Here, we've saved the data to a dataframe object named crimes

DataFrames contain our data in little "spreadsheet"-like structures. Whatever manipulations you can think of doing to the data, you can likely search how to do

Exploring dataframes

Let's take a look at the data. We'll use the function .head() to read in the first 5 rows

How big is the dataset? .shape returns a tuple with the dimensions as (rows, columns)

Let's try to understand our data a bit better.

Show the most recent crime by sorting the dataframe:

Subsetting

Subsetting is a super helpful tool. We'll take a look at this more depth in next week, but for now, here are the basics:

We can filter rows from a dataframe based on some condition

How would you show only crimes north of the Museum of Fine Arts in Boston (Lat > 42.3394)

Hint: Same way as matching if statements in python, mirroring the syntax above

Data Manipulation

What is the percentage of crimes where a shooting occurred?

Visualization

First things first, let's import the package to help us visualize the data, plotly.

If this package isn't yet included, we can install it using !pip install plotly. More on this week 5.

Note that we're using the sub package of the broader package, called plotly express. This simplifies a lot of the more difficult steps

Plotly express has a broad range of options to play with, let's take a look at the documentation.
Do a quick google search to pull up documentation for px.scatter OR run px.scatter? in a Jupyter cell

Let's look at the top ten most frequent crimes

Look at the stacked bar graph. What does the data tell you?

Geographic Plots

Let's take this data into a geographic plot. These crimes happen at certain PLACES, so let's see if we can find a trend.

Skim Over These Articles...

Does this paint a different light on the data?

So, What's the Takeaway?